package com.chuangketiebeta;

import java.util.Stack;

import android.app.Activity;

public class AppManager {
	private static Stack<Activity> mActivityStack ;
	private static AppManager mAppManager;
	
	/**
	 * 单例模式
	 * @return
	 */
	public static AppManager getInstance(){
//		if(mAppManager != null){
			mAppManager = new AppManager();
//		}
		return mAppManager;
		
	}
	/**
	 * 添加Activity到堆栈
	 * @param mActivity
	 */
	public void addActivity(Activity mActivity){
		if(null ==mActivityStack){
			mActivityStack = new Stack<Activity>();
		}
		mActivityStack.add(mActivity);
	}
	/**
	 * 获取栈顶Activity（堆栈中最后一个压入的）
	 * @return
	 */
	public  Activity getTopActivity(){
		Activity activity = mActivityStack.lastElement();
		return activity;
	}
	
	/**
	 * 结束栈顶Activity（堆栈中最后一个压入的）
	 */
	public  void killTopActivity() {
		Activity activity = mActivityStack.lastElement();
		killActivity(activity);
	}
	/**
	 * 结束指定的Activity
	 */
	public void killActivity(Activity activity) {
		if (activity != null) {
			mActivityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}
	/**
	 * 结束指定类名的Activity
	 */
	public  void killActivity(Class<?> cls){
		for(Activity activity:mActivityStack){
			if(mActivityStack.getClass().getName().equals(cls)){
				mActivityStack.remove(activity);
			}
		}
		
	}
	/**
	 * 结束所有的Activity
	 */
	public  void KillAllActivity(){
		for(int i=0 , size = mActivityStack.size();i<size;i++){
			if(mActivityStack.get(i) != null){
				mActivityStack.get(i).finish();
			}
		}
		mActivityStack.clear();
	}
	/**
	 * 退出应用程序
	 */
	public  void ExitApp(){
		try {
			KillAllActivity();
			System.exit(0);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
}
